package design.patterns.behavioral;

/**
 * 策略类
 * 解释：
 * 策略模式是一种行为设计模式， 它能让你定义一系列算法，
 * 并将每种算法分别放入独立的类中， 以使算法的对象能够相互替换。
 */


/**
 * 情景
 * 选择步行、自行车 到达某地 需要的时间
 * 距离单位为 km
 * 时间单位为 min
 * 速度单位为 km/min
 */
public class H_Strategy {
    public static void main(String[] args) {
        Double distance = 100d;
        Walk walk = new Walk(10d);
        Bicycle bicycle = new Bicycle(20d);

        new TakeTime(distance,walk).needTime();

        new TakeTime(distance,bicycle).needTime();

    }
}

abstract class TransportationStrategy{
    protected Double speed = 0d;

    public TransportationStrategy(Double speed) {
        this.speed = speed;
    }

    abstract Double strategySpeedAndNotes();
}
class Walk extends TransportationStrategy {

    public Walk(Double speed) {
        super(speed);
    }
    
    @Override
    public Double strategySpeedAndNotes() {
        System.out.println("带好充足的水和食物");
        return this.speed;
    }
}

class Bicycle extends TransportationStrategy {

    public Bicycle(Double speed) {
        super(speed);
    }

    @Override
    public Double strategySpeedAndNotes() {
        System.out.println("请确保车况良好，带好维修工具");
        return this.speed;
    }
}
class TakeTime{
    private Double distance;
    private TransportationStrategy strategy;

    public TakeTime(Double distance, TransportationStrategy strategy) {
        this.distance = distance;
        this.strategy = strategy;
    }
    public void needTime(){
        System.out.println(distance/strategy.speed);
    }
}